/**
 * @info 校验权限， 根据传入的权限和用户所拥有的权限进行对比，返回对比结果
 * @param permission 当前元素需要的权限
 * */
import { usePermissionStoreHook } from "/@/store/modules/permission";

export const checkPermissions = (permission: string | string[]): boolean => {
  if (!permission) return true;
  let hasAuth = false;
  if (Array.isArray(permission)) {
    for (const item of permission) {
      hasAuth = usePermissionStoreHook().buttonAuth.includes(item);
      if (hasAuth) {
        break;
      }
    }
  } else {
    hasAuth = usePermissionStoreHook().buttonAuth.includes(permission);
  }
  return hasAuth;
};

/**
 * @info 将路由转换为树型结构
 * @param routes 路由数组 usePermissionStoreHook().wholeRoutes;
 * */
import { i18n } from "../plugins/i18n";
const { t } = i18n.global;
export function generateRouteData(routes) {
  const data = [];
  for (const route of routes) {
    const tmp = {
      children: [],
      title: "",
      name: "",
      path: ""
    };
    tmp.title = t((route.meta && route.meta.title) || "").toString();
    tmp.name = route.name;
    tmp.path = route.path;
    if (route.children) {
      tmp.children = generateRouteData(route.children);
    }
    data.push(tmp);
  }
  return data;
}
/**
 * @info 将路由转换为树型结构
 * @param RouteData 路由数组 generateRouteData() 输出的数组;
 * */
export function generateTreeData(RouteData) {
  const data = [];
  for (const route of RouteData) {
    let tmp = {
      children: [],
      title: "",
      name: "",
      path: ""
    };
    if (route.children?.length === 1) {
      tmp = { ...route.children[0] };
    } else {
      tmp = { ...route };
    }
    data.push(tmp);
  }
  return data;
}
